/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/



import request from '@/utils/request';
import {
  IBackgroundCreateParams,
  IBackgroundEditParams,
  IBackgroundItem,
  IBackgroundList,
  IBackgroundListParams,
  IBackgroundStatus,
} from '@/types/background';
import { IPromiseError, IPromiseListApi } from '@/types/type';
import { paginationParams } from '@/utils/soa/utils';
import base from './base';

// 新增背景图
export async function createBackground(data: IBackgroundCreateParams) {
  return await request.post<IPromiseError>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/background`,
    {
      data,
    },
  );
}

// 修改背景图
export async function editBackground(data: IBackgroundEditParams, id: number) {
  return await request.put<IPromiseError>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/background`,
    {
      data: {
        ...data,
        id,
      },
    },
  );
}
// 删除背景图
export async function deleteBackground(id: number) {
  return await request.delete<IPromiseError>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/background`,
    {
      params: {
        id,
      },
    },
  );
}

//列表背景图
export async function getBackgroundList({ current, pageSize }: IBackgroundListParams) {
  const { data } = await request.get<IPromiseListApi<IBackgroundList>>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/background/page`,
    {
      params: {
        ...paginationParams(current, pageSize),
      },
    },
  );
  return {
    list: data?.itemSize
      ? data?.dataList.map((item: IBackgroundItem, index: number) => ({
          ...item,
          index: index + 1 + (current - 1) * pageSize,
        }))
      : [],
    total: data?.pageCount,
  };
}

//背景图状态
export async function editBackgroundStatus(status: IBackgroundStatus, id: number) {
  return await request.put<IPromiseError>(
    `/${base.API}/ms-soa-scene/${base.VERSION}/admin/background/status`,
    {
      data: {
        status,
        id,
      },
    },
  );
}
